using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Xml;

namespace Canvas.Base
{
    /// <summary>
    /// Don vi diem tren man hinh
    /// La te bao tao ra cac doi tuong do hoa, dung trong viec tom, bat diem...
    /// </summary>
    public class UnitPoint : Canvas.CanvasInterfaces.IUnitPoint
    {
        #region Property
        public static UnitPoint Empty; //dinh nghia 1 diem rong
        private double l_x; //toa do x
        private double l_y; //toa do y
        /// <summary>
        /// toa do X
        /// </summary>
        public double X
        {
            get { return l_x; }
        }
        /// <summary>
        /// Toa do Y
        /// </summary>
        public double Y
        {
            get { return l_y; }
        }

        public PointF Point
        {
            get { return new PointF((float)l_x, (float)l_y); }
        }
        #endregion

        #region contructor
        public UnitPoint()
        {
            Empty = new UnitPoint();
            Empty.l_x = double.NaN;
            Empty.l_y = double.NaN;
        }
        public UnitPoint(double pX, double pY)
        {
            l_x = pX;
            l_y = pY;
        }
        public UnitPoint(PointF pPoint)
        {
            l_x = pPoint.X;
            l_y = pPoint.Y;
        }
        #endregion

        #region Method
        /// <summary>
        /// so sanh !=
        /// </summary>
        /// <param name="left">diem thu nhat (cho la left)</param>
        /// <param name="right">diem thu hai</param>
        /// <returns></returns>
        public static bool operator !=(UnitPoint left, UnitPoint right)
        {
            if (left.X == right.X)
                return left.Y == right.Y;
            if (left.IsEmpty && right.IsEmpty) // after changing Empty to use NaN this extra check is required
                return true;
            return false;
        }
        /// <summary>
        /// so sanh ==
        /// </summary>
        /// <param name="left">diem thu nhat (cho la left)</param>
        /// <param name="right">diem thu hai</param>
        /// <returns></returns>
        public static bool operator ==(UnitPoint left, UnitPoint right)
        {
            if (left.X == right.X)
                return left.Y == right.Y;
            if (left.IsEmpty && right.IsEmpty) //--> kiem tra khi left, right deu la Empty
                return true;
            return false;
        }

        public static UnitPoint operator +(UnitPoint left, UnitPoint right)
        {
            left.l_x += right.l_x;
            left.l_y += right.l_y;
            return left;
        }

        public bool IsEmpty
        {
            get
            {
                return double.IsNaN(l_x) && double.IsNaN(l_y);
            }
        }
        
        public override string ToString()
        {
            return string.Format("{{X={0}, Y={1}}}", XmlConvert.ToString(Math.Round(X, 8)), XmlConvert.ToString(Math.Round(Y, 8)));
        }
        public override bool Equals(object obj)
        {
            if (obj is UnitPoint)
                return (this == (UnitPoint)obj);
            return false;
        }
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
        /// <summary>
        /// Vi tri cua diem tren man hinh
        /// </summary>
        /// <returns></returns>
        public string PositionAsString()
        {
            return string.Format("[{0:f4},{1:f4}]", X, Y);
        }
        #endregion

      
    }
}
